// pages/ceshi/ceshi.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    awardsList: [
      { 'index': 0, 'name': '1元红包' },
      { 'index': 1, 'name': '5元话费' },
      { 'index': 2, 'name': '6元红包' },
      { 'index': 3, 'name': '8元红包' },
      { 'index': 4, 'name': '10元话费' },
      { 'index': 5, 'name': '10元红包' }],
    animationData:{},
    stopClickFlag: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.animationInit = null;
    this.animationRun = null;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;

    // 绘制转盘
    var awardsConfig = this.data.awardsList,
      len = awardsConfig.length,
      rotateDeg = 360 / len / 2 + 90,
      html = [],
      turnNum = 1 / len;  // 文字旋转 turn 值

    for (var i = 0; i < len; i++) {
      // 奖项列表
      html.push({ turn: i * turnNum + 'turn', award: awardsConfig[i].name });
    }
    that.setData({
      awardsList: html
    });
    console.log(this.data.awardsList)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  startLuck:function(){
      console.log("开始");
      let awardIndex = 3;

      //一旦开始,不允许再点击
      this.setData({
        stopClickFlag:true
      });

      setTimeout(() => {
        this.animationRun = wx.createAnimation({
          duration: 2000,
          timingFunction: 'ease'
        })
        this.animationRun.rotate(360 * 8 - awardIndex * (360 / 6)).step();
        this.setData({
          animationData: this.animationRun.export()
        })
      }, 100);
      
      setTimeout(() => {
        wx.showModal({
          title: '恭喜',
          content: '获得' + (this.data.awardsList[awardIndex].award),
          showCancel: false,
          success:(res) => {
            if (res.confirm){

              this.setData({
                stopClickFlag: false
              });

              this.animationInit = wx.createAnimation({
                duration: 1
              });
              this.animationInit.rotate(0).step();
              this.setData({
                animationData: this.animationInit.export()
              });
            }
          }
        });
        
        
      }, 2100);
  }
})